package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.cost.TaskBaselineEntity;
import com.hllxd.hism.star.basic.service.service.TaskBaselineService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/task_baseline")
public class TaskBaselineController2 {
    @Autowired
    private TaskBaselineService taskBaselineService;

    @Data
    public static class Params {
        public Long id;
        public String ids;
    }
   
    @RestGrpcMapping("/listTaskBaseline.do")
    public NTSTATUS listTaskBaseline(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            List<TaskBaselineEntity> res;
            res = taskBaselineService.listTaskBaseline();
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getTaskBaseline.do")
    public NTSTATUS getTaskBaseline(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            Params aa = params.driverObject(Params.class);
            TaskBaselineEntity res;
            res = taskBaselineService.getTaskBaseline(aa.id);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/setTaskBaseline.do")
    public NTSTATUS saveTaskBaseline(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            TaskBaselineEntity aa = params.driverObject(TaskBaselineEntity.class);
            long res;
            res = taskBaselineService.setTaskBaseline(aa);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/delTaskBaseline.do")
    public NTSTATUS delTaskBaseline(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            Params aa = params.driverObject(Params.class);
            taskBaselineService.delTaskBaseline(aa.ids);
            result.setData("success");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
